<script setup>
import { ref,onMounted } from 'vue';
import {getCaptcharService} from "@/api/captcha";
import { ElMessage, ElForm, ElInput, ElButton, ElCard } from 'element-plus';
import {user} from "@/stores/counter.js";
import router from "@/router/index.js";

// 表单数据
const loginForm = ref({
  username: '',
  password: '',
  code:'',
  token:''
});
const imageUrl = ref("");
const getCode = async () => {
  let result = await getCaptcharService();
  console.log(result);
  imageUrl.value = "data:image/gif;base64," + result.data.base64Img;
  console.log(imageUrl.value);
  loginForm.value.token = result.data.token;
};
const formRef = ref(null);
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
};
const userInfo=user();
const login = async () => {
  formRef.value.validate((valid) => {
    if (!valid) {
      return;
    }
    userInfo.login(loginForm.value).then((res) => {
      ElMessage.success(res.data);
      router.push("/home");
    }).catch((err) => {
      ElMessage.error(err);
    });
  });

};
onMounted(async () => {
  await getCode();
})
</script>

<template>
  <div class="login-container">
    <el-card class="login-card" shadow="always">
      <div style="display: flex;flex-direction: row;align-items: center;text-align: center;justify-content: center;">
        <img src="../assets/img/qiqi2.png" style="width: 30px;margin: 0 5px;padding-top: 5px" class="login-img"/>
        <h2 >不卜庐养生堂管理系统</h2>
      </div>
     
      <el-divider />
      <el-form ref="formRef"  :model="loginForm" :rules="rules" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" style="width: 75%;"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" style="width: 75%;"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="loginForm.code" placeholder="请输入验证码" style="width: 75%;"></el-input>
          <img :src="imageUrl" alt="验证码" @click="getCode" style="margin-top: 10px;"/>
        </el-form-item>
       <div class="btn-container">
          <el-button type="primary" @click="login">登录</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-card {
  width: 400px;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
}
.el-button--primary {
  background-color: #c8d7b4;
  color: #3d4f2f;
  border-color: #c8d7b4;
  width: 70%;
  margin: auto;
}

.btn-container{
  display: flex;
  justify-content: center; 
  margin-top: 20px;
}
</style>